package com.peng.leetcode.other;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * FindMedianSortedArrays
 *
 * @author: lupeng6
 * @create: 2020/10/9 18:16
 */
public class FindMedianSortedArrays {

    public static void main(String[] args) {
        int[] nums1 = new int[]{1, 2};
        int[] nums2 = new int[]{3, 4};
        System.out.println(new Solution().findMedianSortedArrays(nums1, nums2));
    }


    static class Solution {
        public double findMedianSortedArrays(int[] nums1, int[] nums2) {
            List<Integer> list = new ArrayList<Integer>(nums1.length + nums2.length);
            for (int i : nums1) {
                list.add(i);
            }
            for (int i : nums2) {
                list.add(i);
            }
            Collections.sort(list);
            int size = list.size();
            if (size % 2 == 0) {
                double median = (list.get((size / 2) - 1) + list.get((size / 2))) / 2.0;
                return median;
            } else {
                double median = list.get(size / 2) / 1.0;
                return median;
            }
        }
    }


    static class Solution1 {
        public double findMedianSortedArrays(int[] nums1, int[] nums2) {
            List<Integer> list = new ArrayList<Integer>(nums1.length + nums2.length);
            for (int i : nums1) {
                list.add(i);
            }
            for (int i : nums2) {
                list.add(i);
            }
            Collections.sort(list);
            int size = list.size();
            if (size % 2 == 0) {
                double median = (list.get((size / 2) - 1) + list.get((size / 2))) / 2.0;
                return median;
            } else {
                double median = list.get(size / 2) / 1.0;
                return median;
            }
        }
    }
}
